package com.wanpinghui.supplier.widgets;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.message.PhotoBrowseActivity;
import com.wanpinghui.supplier.utils.BitmapUtilManager;
import com.wanpinghui.wphlibrary.model.message.bean.MessagesExt;
import com.wanpinghui.wphlibrary.model.message.bean.MsgBean;
import com.yunshl.yunshllibrary.utils.DevicesUtil;
import com.yunshl.yunshllibrary.utils.TextUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Random;

/**
 * Created by xiongning on 17-12-7.
 */

public class MsgLeftImgView extends LinearLayout {

    private RelativeLayout rl_msg_content;

    private ImageView iv_msg;

    private RelativeLayout rl_loading;

    private SpinView sv;

    private TextView tv_percent;

    private ImageView iv_error;

    private MsgBean msgBean;

    private Handler mHandler = null;

    private int current = 0;

    public MsgLeftImgView(Context context) {
        super(context);
        init(context);
    }

    public MsgLeftImgView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MsgLeftImgView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public MsgLeftImgView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(final Context context) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mView = inflater.inflate(R.layout.item_msg_left_img, this);
        rl_msg_content = (RelativeLayout) mView.findViewById(R.id.rl_msg_content);
        iv_msg = (ImageView) mView.findViewById(R.id.iv_msg);
        rl_loading = (RelativeLayout) mView.findViewById(R.id.rl_loading);
        sv = (SpinView) mView.findViewById(R.id.sv);
        tv_percent = (TextView) mView.findViewById(R.id.tv_percent);
        iv_error = (ImageView) mView.findViewById(R.id.iv_error);
        rl_msg_content.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (msgBean.getMsg() != null && msgBean.getMsg().length() > 0) {
                    ArrayList<String> urlList = new ArrayList<>();
                    urlList.add(msgBean.getMsg());
                    PhotoBrowseActivity.startWithElement((Activity) getContext(), urlList, 0, v, msgBean.getExt());
                }
            }
        });
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                updatePercent();
            }
        };
    }

    private void updatePercent() {
        current = current + new Random().nextInt(10);
        if (current >= 100)
            current = 99;
        setPercent(current);
        mHandler.sendEmptyMessageDelayed(0, 500);
    }

    public void initData(MsgBean msgBean) {
        this.msgBean = msgBean;
        MessagesExt ext = MessagesExt.getMessagesExt(msgBean.getExt());
        setMsg(msgBean.getMsg());
        setPercent(0);
        switch (msgBean.getSend_statu()) {
            case 0:
                setError(false);
                setSending(true);
                mHandler.sendEmptyMessage(0);
                break;
            case 1:
                mHandler.removeMessages(0);
                setPercent(100);
                setError(false);
                setSending(false);
                break;
            case 2:
                mHandler.removeMessages(0);
                setError(true);
                setSending(false);
                break;
        }
    }

    public void setMsg(String msg) {
        MessagesExt ext = MessagesExt.getMessagesExt(msgBean.getExt());
        String path = null;
        if (TextUtil.isNotEmpty(ext.getFile_path()) && new File(ext.getFile_path()).exists()) {
            path = ext.getFile_path();
        } else {
            if (msg != null && msg.length() <= 0) {
                iv_msg.setLayoutParams(new RelativeLayout.LayoutParams(DevicesUtil.dip2px(getContext(), 133f), DevicesUtil.dip2px(getContext(), 99f)));
                iv_msg.setImageDrawable(getResources().getDrawable(R.mipmap.message_icon_load_fail_right));
                return;
            }
            path = msg + "_thumbnail";
        }

        BitmapUtilManager.getInstance().loadImage(getContext(), iv_msg, path, 133, rl_loading);
    }

    public void setPercent(int count) {
        tv_percent.setText(count + "%");
    }

    public void setSending(boolean isSendding) {
        if (isSendding)
            rl_loading.setVisibility(View.VISIBLE);
        else
            rl_loading.setVisibility(View.GONE);

    }

    public void setError(boolean isError) {
        if (isError)
            iv_error.setVisibility(View.VISIBLE);
        else
            iv_error.setVisibility(View.GONE);
    }
}
